package net.zhengxinyun.performance.data;

import com.alibaba.fastjson.JSONArray;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.Util.WorkbookUtil;
import net.zhengxinyun.performance.entity.CountryScoreEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.service.indicators.CountryIndicatorsService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 市对县分数上传
 * Created by hyp1202 on 2019/2/22.
 */
@Component
public class CountryScoreData {

    private CountryIndicatorsService countryIndicatorsService;

    @Autowired
    public CountryScoreData(CountryIndicatorsService countryIndicatorsService) {
        this.countryIndicatorsService = countryIndicatorsService;
    }

    private static final String COL_INDEX_NAME= "二级指标名";
    private static final String COL_YEAR = "年份";
    private static final String COL_MONTH = "月份";
    private static final String COL_SCORE = "分数";

    private static final String[] LABLE_ZERO = {"二级指标名","年份","月份","分数"};

    public List<CountryScoreEntity> importCountryScore(String filePath) throws Exception{
        List<CountryScoreEntity> list = new ArrayList<>();
        List<String> errors = new ArrayList<>();
        Workbook book = WorkbookUtil.getWorkbook(filePath);
        Sheet sheet = book.getSheetAt(0);
        Iterator<Cell> iterator = sheet.getRow(0).cellIterator();
        int length = 0;
        List<String> lableListZero = new ArrayList<>();
        while (iterator.hasNext()){
            String str = iterator.next().getStringCellValue();
            if (!"".equals(str)){
                lableListZero.add(str);
                if (length > 4){
                    throw new Exception("请选择正确模板上传");
                }
                length++;
            }
        }
        if (!Util.arrayEquals(lableListZero.toArray(),LABLE_ZERO)){
            throw new Exception("请选择正确模板上传");
        }
        for (int rowId = 1;rowId <= sheet.getLastRowNum();rowId++){
            Row row = sheet.getRow(rowId);
            if (WorkbookUtil.isRowEmpty(row)){
                continue;
            }
            try {
                CountryScoreEntity countryScoreEntity = new CountryScoreEntity();

                String indexName = WorkbookUtil.getStringCellValue(row,COL_INDEX_NAME);
                if (StringUtils.isBlank(indexName)){
                    throw new Exception("指标名不能为空");
                }
                String year = WorkbookUtil.getStringCellValue(row,COL_YEAR);
                if (StringUtils.isBlank(year)){
                    throw new Exception("年份不能为空");
                }
                else {
                    CountryIndicatorsFilter countryIndicatorsFilter = new CountryIndicatorsFilter();
                    countryIndicatorsFilter.setIndexName(indexName);
                    countryIndicatorsFilter.setDateTime(year);
                    countryIndicatorsFilter = countryIndicatorsService.query(countryIndicatorsFilter).get(0);
                    countryScoreEntity.setIndicatorsId(countryIndicatorsFilter.getId().toString());
                }
                String month = WorkbookUtil.getStringCellValue(row,COL_MONTH);
                if (StringUtils.isBlank(month)){
                    throw new Exception("月份不能为空");
                }else {
                    countryScoreEntity.setMonthTime(month);
                }
                String score = WorkbookUtil.getStringCellValue(row,COL_SCORE);
                if (StringUtils.isBlank(score)){
                    throw new Exception("分数不能为空");
                }else {
                    countryScoreEntity.setScore(score);
                }
                countryScoreEntity.setAudit("0");
                list.add(countryScoreEntity);
            }catch (Exception e){
                errors.add("第"+rowId+"行："+e.getMessage());
            }
        }
        if (errors.isEmpty())
            throw new Exception(JSONArray.toJSONString(errors));
        return list;
    }
}
